package cn.kgc.mes.productrecpt.service.impl;

import java.math.BigDecimal;
import java.util.List;
import cn.kgc.mes.common.utils.DateUtils;
import cn.kgc.mes.productreceptline.domain.ProductRecptLine;
import cn.kgc.mes.productreceptline.service.IProductRecptLineService;
import cn.kgc.mes.productreceptline.service.impl.ProductRecptLineServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.kgc.mes.productrecpt.mapper.ProductRecptMapper;
import cn.kgc.mes.productrecpt.domain.ProductRecpt;
import cn.kgc.mes.productrecpt.service.IProductRecptService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品入库录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-19
 */
@Service
public class ProductRecptServiceImpl implements IProductRecptService 
{
    @Autowired
    private ProductRecptMapper productRecptMapper;
    @Autowired
    private IProductRecptLineService productRecptLineService;
    /**
     * 查询产品入库录
     * 
     * @param recptId 产品入库录主键
     * @return 产品入库录
     */
    @Override
    public ProductRecpt selectProductRecptByRecptId(Long recptId)
    {
        return productRecptMapper.selectProductRecptByRecptId(recptId);
    }

    /**
     * 查询产品入库录列表
     * 
     * @param productRecpt 产品入库录
     * @return 产品入库录
     */
    @Override
    public List<ProductRecpt> selectProductRecptList(ProductRecpt productRecpt)
    {
        return productRecptMapper.selectProductRecptList(productRecpt);
    }

    /**
     * 新增产品入库录
     * 
     * @param productRecpt 产品入库录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProductRecpt(ProductRecpt productRecpt)
    {
        productRecpt.setCreateTime(DateUtils.getNowDate());
        int a = productRecptMapper.insertProductRecpt(productRecpt);
        System.out.println("返回的id"+productRecpt.getRecptId());
        ProductRecptLine productRecptLine = new ProductRecptLine();
        //设置子组件的父组件id以及其他值
        productRecptLine.setRecptId(productRecpt.getRecptId());
        productRecptLine.setWorkorderId(productRecpt.getWorkorderId());
        productRecptLine.setWorkorderCode(productRecpt.getWorkorderCode());
        productRecptLine.setWorkorderName(productRecpt.getWorkorderName());
        productRecptLine.setItemId(productRecpt.getItemId());
        productRecptLine.setItemCode(productRecpt.getItemCode());
        productRecptLine.setItemName(productRecpt.getItemName());
        productRecptLine.setSpecification(productRecpt.getSpecification());
        productRecptLine.setUnitOfMeasure(productRecpt.getUnitOfMeasure());
        // 转化逻辑
        String attr1Str = productRecpt.getAttr1();
        BigDecimal result = null;
        try {
            // 1. 空值处理：转为 null 或默认值（根据业务选其一）
            if (attr1Str == null || attr1Str.trim().isEmpty()) {
                // 方案A：空值返回 null
                result = null;
                // 方案B：空值返回默认值（如 0.00）
                // result = BigDecimal.ZERO;
            } else {
                // 2. 字符串转 BigDecimal（推荐用 String 构造器，避免精度丢失）
                result = new BigDecimal(attr1Str.trim());
                // 可选：设置精度（如保留2位小数，四舍五入）
                // result = result.setScale(2, RoundingMode.HALF_UP);
            }
        } catch (NumberFormatException e) {
            // 3. 格式异常处理（如字符串不是数值）
            System.err.println("attr1 格式错误，无法转为 BigDecimal：" + attr1Str);
            // 异常时返回默认值或抛业务异常
            result = BigDecimal.ZERO;
            // throw new BusinessException("数值格式错误：" + attr1Str); // 按业务需求选
        }
        productRecptLine.setQuantityRecived(result);
        //执行子组件的保存
        int b = productRecptLineService.insertProductRecptLine(productRecptLine);
        return a+b;
    }

    /**
     * 修改产品入库录
     * 
     * @param productRecpt 产品入库录
     * @return 结果
     */
    @Override
    public int updateProductRecpt(ProductRecpt productRecpt)
    {
        productRecpt.setUpdateTime(DateUtils.getNowDate());
        return productRecptMapper.updateProductRecpt(productRecpt);
    }

    /**
     * 批量删除产品入库录
     * 
     * @param recptIds 需要删除的产品入库录主键
     * @return 结果
     */
    @Override
    public int deleteProductRecptByRecptIds(Long[] recptIds)
    {
        return productRecptMapper.deleteProductRecptByRecptIds(recptIds);
    }

    /**
     * 删除产品入库录信息
     * 
     * @param recptId 产品入库录主键
     * @return 结果
     */
    @Override
    public int deleteProductRecptByRecptId(Long recptId)
    {
        return productRecptMapper.deleteProductRecptByRecptId(recptId);
    }
}
